package jh.com.jhappbf.dispatchcar;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbLoadDialogFragment;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.android.volley.Request.Priority;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.yzq.zxinglibrary.android.CaptureActivity;
import com.yzq.zxinglibrary.common.Constant;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import androidx.core.content.ContextCompat;
import jh.com.jhappbf.ComfirmListAdapter;
import jh.com.jhappbf.Interface.GetItemClick;
import jh.com.jhappbf.R;
import jh.com.jhappbf.httputils.VolleyCallWebService;
import jh.com.jhappbf.httputils.VolleySingleton;
import jh.com.jhappbf.model.DispatchModel;
import jh.com.jhappbf.model.PlanModel;
import jh.com.jhappbf.platenumber.PlateNumberSelect;
import jh.com.jhappbf.ui.State;
import jh.com.jhappbf.utils.JsonHelper;
import jh.com.jhappbf.view.ImageDialog;
import jh.com.jhappbf.weight.query.SelectActivity;

public class DispatchCarAdd extends AbActivity implements OnClickListener, GetItemClick {
    private static final int UNLOADAREA = 0x009;
    private static final int UNLOADUNIT = 0x011;
    private ImageView mPlatenumberImg;
    private EditText mPlatenumberTxt;//车号

    private EditText mCargonameTxt;//货名
    private ImageView mCargonameImg;

    private EditText mSpecTxt;//规格
    private ImageView mSpecImg;

    private EditText mTypeTxt;//类型
    private ImageView mTypeImg;

    private EditText mSenderTxt;//发货
    private ImageView mSenderImg;

    private EditText mReceiverTxt;//收货
    private ImageView mReceiverImg;

    // private EditText mRawNetTxt;
    private EditText mOperatorTxt;//操作人

    private ImageView mPriceImg;
    private EditText mPriceTxt;//单价
    private EditText mBeizuTxt;
    private EditText mBagNetTxt;
    private EditText mOrderId;//派车单号
    private EditText mPlanId;//订单号
    private EditText mRawNet;//预装量
    private EditText mUnloadArea;//库位
    private EditText mDriverName;//司机姓名
    private EditText mIDNumber;//司机身份证号
    private EditText mDriverPhone;//司机手机号

    //    private EditText mContractTxt;
    private ImageView mUnloadAreaImg;//库位选择

    private LinearLayout mBeizuLayout;
    private LinearLayout mBagNetLayout;
    private Button mSaveBtn;
    private Button mCleanBtn;
    private Button mAddDispatchBtn;
    private final int GETCARNUMBER = 0X001;
    private final int CARGONAME = 0X002;
    private final int SPEC = 0X003;
    private final int TYPE = 0X004;
    private final int SENDER = 0X005;
    private final int RECEIVER = 0X006;
    private final int TRANSPORT = 0X007;
    private final int GETPLAN = 0X008;
    private final int TOCODE = 0X010;

    private Context mAppContext;
    private AbTitleBar mAbTitleBar;
    private String mFlag;
    private String contractId;
    private String codeContent;
    private ListView mListView;
    private ArrayList<String> mData;//TextView名称集合
    private ArrayList<String> mDataZiDuan;//TextView名称集合
    private ComfirmListAdapter mAdapter;
    private GetItemClick getItemClick=this::getImgClick;

    private AbLoadDialogFragment mDialogFragment;
    private ImageView mPlanIdImg;
    private EditText mUnloadUnit;//组织单位
    private ImageView mUnloadUnitImg;
    private CheckBox isLong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.dispatch_car_add_activity);
        mAbTitleBar = this.getTitleBar();
        mAbTitleBar.setTitleText("派车信息");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        mData=new ArrayList<>();
        mDataZiDuan=new ArrayList<>();
        initView();
    }

    /**
     * 初始化
     */
    private void initView() {

        mOrderId = findViewById(R.id.dispatch_car_add_id_edit);
        mPlanId = findViewById(R.id.dispatch_car_add_plan_edit);
        mPlanIdImg = findViewById(R.id.dispatch_car_add_plan_select);
        mPlanIdImg.setOnClickListener(this);
        mRawNet = findViewById(R.id.dispatch_car_add_rawNet_edit);
        mUnloadArea = findViewById(R.id.dispatch_car_add_area_edit);
        mUnloadUnit = findViewById(R.id.dispatch_car_add_unit_edit);
        mDriverName = findViewById(R.id.dispatch_car_add_driver_edit);
        mIDNumber = findViewById(R.id.dispatch_car_add_identity_edit);
        mDriverPhone = findViewById(R.id.dispatch_car_add_phone_edit);
        mAppContext = getApplicationContext();

//        View v = getLayoutInflater().inflate(R.layout.title_right_ok_btn_view, null);
//        mAddDispatchBtn = v.findViewById(R.id.title_right_ok_btn);
//        mAbTitleBar.addRightView(v);
//        mAddDispatchBtn.setOnClickListener(this);

//        mContractTxt = findViewById(R.id.dispatch_car_add_unit_edit);
        mUnloadAreaImg = findViewById(R.id.dispatch_car_add_area_select);
        mUnloadUnitImg = findViewById(R.id.dispatch_car_add_unit_select);
//        mContractTxt.setOnClickListener(this);
        mUnloadAreaImg.setOnClickListener(this);
        mUnloadUnitImg.setOnClickListener(this);

        mPlatenumberImg = findViewById(R.id.dispatch_car_add_platenumber_select);
        mPlatenumberTxt = findViewById(R.id.dispatch_car_add_platenumber_edit);
        mPlatenumberImg.setOnClickListener(this);
        mPlatenumberTxt.setOnClickListener(this);

        mCargonameImg = findViewById(R.id.dispatch_car_add_cargoname_select);
        mCargonameTxt = findViewById(R.id.dispatch_car_add_cargoname_edit);
        mCargonameImg.setOnClickListener(this);

        mSpecImg = findViewById(R.id.dispatch_car_add_spec_select);
        mSpecTxt = findViewById(R.id.dispatch_car_add_spec_edit);
        mSpecImg.setOnClickListener(this);

        mTypeImg = findViewById(R.id.dispatch_car_add_type_select);
        mTypeTxt = findViewById(R.id.dispatch_car_add_type_edit);
        mTypeImg.setOnClickListener(this);

        mSenderImg = findViewById(R.id.dispatch_car_add_sender_select);
        mSenderTxt = findViewById(R.id.dispatch_car_add_sender_edit);
        mSenderImg.setOnClickListener(this);

        mReceiverImg = findViewById(R.id.dispatch_car_add_receiver_select);
        mReceiverTxt = findViewById(R.id.dispatch_car_add_receiver_edit);
        mReceiverImg.setOnClickListener(this);

        // mRawNetTxt = (EditText)
        // findViewById(R.id.dispatch_car_add_rawNet_edit);
        mOperatorTxt = findViewById(R.id.dispatch_car_add_operator_edit);

        mPriceImg = findViewById(R.id.dispatch_car_add_price_select);
        mPriceImg.setOnClickListener(this);
        mPriceTxt = findViewById(R.id.dispatch_car_add_price_edit);
        isLong=findViewById(R.id.isLong);

        mBeizuTxt = findViewById(R.id.dispatch_car_add_beizu__edit);
        mBagNetTxt = findViewById(R.id.dispatch_car_add_bagNet_edit);
        mBeizuLayout = findViewById(R.id.didpatch_car_beizu_layout);
        mBagNetLayout = findViewById(R.id.dispatch_car_bag_layout);

        mSaveBtn = findViewById(R.id.dispatch_car_add_btn_ok);
        mSaveBtn.setOnClickListener(this);
        mCleanBtn = findViewById(R.id.dispatch_car_add_btn_refresh);
        mCleanBtn.setOnClickListener(this);
        mOperatorTxt.setText(State.mName);

        mListView = (ListView) findViewById(R.id.add_list_view);
        mAdapter = new ComfirmListAdapter(this,mData,mDataZiDuan,this);
        mListView.setAdapter(mAdapter);

        setOrderId();
        getValueFromIntent();
    }

    public static void reSetListViewHeight(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))+200;
        listView.setLayoutParams(params);
    }
    private void setOrderId() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS", Locale.getDefault());
        String orderId = simpleDateFormat.format(System.currentTimeMillis());
        mOrderId.setText(orderId);
    }

    /**
     * 获取传入的数据
     */
    private void getValueFromIntent() {
        Intent i = getIntent();
        mFlag = i.getStringExtra("flag");
        DispatchModel model = i.getParcelableExtra("model");
        if (model != null) {
            mPlatenumberTxt.setTag(model.getDispatchId());
            mPlatenumberTxt.setText(model.getPlateNumber());
//            mContractTxt.setText(model.getUnit());
//            mContractTxt.setTag(model.getPlanId());
            mPlanId.setText(model.getPlanId());
            mCargonameTxt.setText(model.getCargoname());
            mSpecTxt.setText(model.getSpec());
            mTypeTxt.setText(model.getType());
            mSenderTxt.setText(model.getSender());
            mReceiverTxt.setText(model.getReceiver());
            mPriceTxt.setText(model.getPrice());
        }

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dispatch_car_add_plan_select:
            case R.id.dispatch_car_add_plan_edit:
                if (TextUtils.isEmpty(mTypeTxt.getText().toString())){
                    AbToastUtil.showToast(this,"请先选择类型");
                    return;
                }
                getPlan();
                break;
            case R.id.dispatch_car_add_platenumber_select:
            case R.id.dispatch_car_add_platenumber_edit:
                Intent i = new Intent(DispatchCarAdd.this, PlateNumberSelect.class);
                startActivityForResult(i, GETCARNUMBER);
                break;
            case R.id.dispatch_car_add_cargoname_select:
                selectValue(CARGONAME, "cargoname");
                break;
            case R.id.dispatch_car_add_spec_select:
                selectValue(SPEC, "spec");
                break;
            case R.id.dispatch_car_add_type_select:
                selectValue(TYPE, "type");
                break;
            case R.id.dispatch_car_add_sender_select:
                selectValue(SENDER, "sender");
                break;
            case R.id.dispatch_car_add_receiver_select:
                selectValue(RECEIVER, "receiver");
                break;
            case R.id.dispatch_car_add_price_select:
                selectTransport(TRANSPORT);
                break;
            case R.id.dispatch_car_add_btn_ok:
                if (TextUtils.isEmpty(mTypeTxt.getText().toString())){
                    AbToastUtil.showToast(this,"请先选择类型");
                    return;
                }
                Intent i2 = new Intent(this, CaptureActivity.class);
                startActivityForResult(i2, TOCODE);

                break;
            case R.id.dispatch_car_add_btn_refresh:
                refresh();
                break;
//            case R.id.dispatch_car_add_unit_edit:
            case R.id.dispatch_car_add_area_select:
                selectValue(UNLOADAREA, "unloadArea");
                break;
            case R.id.dispatch_car_add_unit_select:
                selectValue(UNLOADUNIT, "unloadUnit");
                break;
            case R.id.title_right_ok_btn:
                operateServer();
                break;

            default:
                break;
        }
    }

    /**
     * 获取计划
     */
    private void getPlan() {
        Intent i = new Intent(DispatchCarAdd.this, DispatchContractSelect.class);
        i.putExtra("type",mTypeTxt.getText().toString());
        startActivityForResult(i, GETPLAN);
    }

    /**
     * 选择数值
     */
    private void selectValue(int requestCode, String flag) {
        Intent i = new Intent(DispatchCarAdd.this, SelectActivity.class);
        i.putExtra("flag", flag);
        startActivityForResult(i, requestCode);
    }

    private void selectTransport(int requestCode) {
        Intent i = new Intent(DispatchCarAdd.this,
                DispatchCarSelectTransport.class);
        startActivityForResult(i, requestCode);
    }

    /**
     * 服务器操作
     */
    private void operateServer() {
        AbLoadDialogFragment mDialogFragment = AbDialogUtil.showLoadDialog(DispatchCarAdd.this,
                R.mipmap.ic_load, "查询中,请稍等...");
        mDialogFragment.setCancelable(false);
        mDialogFragment.setAbDialogOnLoadListener(() -> {
            if ("update".equals(mFlag)) {
                updateDispatch();

            } else {
                addDispatch();
            }
        });
    }

    /**
     * 添加派车
     */
    private void addDispatch() {

//        String planId = String.valueOf(mContractTxt.getTag());
        String planid = mPlanId.getText().toString();
        String orderid = mOrderId.getText().toString();
        String platenum = mPlatenumberTxt.getText().toString();
        String cargoname = mCargonameTxt.getText().toString();
        String spec = mSpecTxt.getText().toString();
        String type = mTypeTxt.getText().toString();
        String sender = mSenderTxt.getText().toString();
        String receiver = mReceiverTxt.getText().toString();
        String price = mPriceTxt.getText().toString();
        String operator = mOperatorTxt.getText().toString();
        String rawNet = mRawNet.getText().toString();
        String unloadArea = mUnloadArea.getText().toString();
        String unloadUnit = mUnloadUnit.getText().toString();
        String unBeizu = mBeizuTxt.getText().toString();
        String driverName = mDriverName.getText().toString();
        String idNumber = mIDNumber.getText().toString();
        String driverPhone = mDriverPhone.getText().toString();

        if (TextUtils.isEmpty(platenum)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "车号不能为空!");
            return;
        }
        if (TextUtils.isEmpty(contractId)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "请选择合同!");
            return;
        }

//        if (TextUtils.isEmpty(rawNet)) {
//            AbDialogUtil.removeDialog(DispatchCarAdd.this);
//            AbToastUtil.showToast(DispatchCarAdd.this, "预装量不能为空!");
//            return;
//        }

        if (TextUtils.isEmpty(cargoname)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "货名不能为空!");
            return;
        }

        if (TextUtils.isEmpty(unloadArea)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "库位不能为空!");
            return;
        }
        if (TextUtils.isEmpty(unloadUnit)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "组织单位不能为空!");
            return;
        }

        if (TextUtils.isEmpty(driverName)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "司机姓名不能为空!");
            return;
        }

        if (TextUtils.isEmpty(idNumber)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "身份证号不能为空!");
            return;
        }

        if (TextUtils.isEmpty(driverPhone)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "手机号不能为空!");
            return;
        }


        mAddDispatchBtn.setEnabled(false);
        mSaveBtn.setEnabled(false);
        ArrayList<String> params = new ArrayList<>();
        params.add("planid");
        params.add("orderid");
        params.add("platenum");
        params.add("cargoname");
        params.add("spec");
        params.add("price");
        params.add("driverName");
        params.add("IDNum");
        params.add("driverPhone");
        params.add("type");
        params.add("sender");
        params.add("receiver");
        params.add("userName");
        params.add("rawNet");
        params.add("unloadArea");
        params.add("unloadUnit");
        params.add("unBeizu");
        params.add("isLong");

        ArrayList<String> values = new ArrayList<>();

        values.add(planid);
        values.add(orderid);
        values.add(platenum);
        values.add(cargoname);
        values.add(spec);
        values.add(price);
        values.add(driverName);
        values.add(idNumber);
        values.add(driverPhone);
        values.add(type);
        values.add(sender);
        values.add(receiver);
        values.add(State.mName);
        values.add(rawNet);
        values.add(unloadArea);
        values.add(unloadUnit);
        values.add(unBeizu);
        if (isLong.isChecked()){
            values.add("1");
        }else {
            values.add("0");
        }
        String methodName = "addDispatchJieXiu";
        Listener<String> listener = response -> {
            mAddDispatchBtn.setEnabled(true);
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            mSaveBtn.setEnabled(true);
            JSONObject object = JsonHelper.getJsonObject(response);
            if (object != null && object.has("result")) {
                String value;
                try {
                    value = object.getString("result");
                    if ("true".equals(value)) {
                        ImageDialog.show(DispatchCarAdd.this, contractId,platenum,cargoname,contractId,mAppContext);
                        AbToastUtil.showToast(DispatchCarAdd.this, "添加成功!");
                        refresh();
                    } else {
                        AbToastUtil.showToast(DispatchCarAdd.this, "添加失败!");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                AbToastUtil.showToast(DispatchCarAdd.this, "添加失败!");
            }
        };

        ErrorListener errorListener = error -> {
            mAddDispatchBtn.setEnabled(true);
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "网络不给力，请重试!");
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                params, values, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(mAppContext).addRequest(webService);
    }

    /**
     * 更新派车单
     */
    private void updateDispatch() {
        String id = String.valueOf(mPlatenumberTxt.getTag());
//        String planId = String.valueOf(mContractTxt.getTag());
        String platenum = mPlatenumberTxt.getText().toString();
//        String unit = mContractTxt.getText().toString();
        String cargoname = mCargonameTxt.getText().toString();
        String spec = mSpecTxt.getText().toString();
        String type = mTypeTxt.getText().toString();
        String sender = mSenderTxt.getText().toString();
        String receiver = mReceiverTxt.getText().toString();
        String price = mPriceTxt.getText().toString();
        String operator = mOperatorTxt.getText().toString();

        if ("null".equals(contractId) || id.length() == 0) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "派车单号不能为空!");
            return;
        }

        if (TextUtils.isEmpty(platenum)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "车号不能为空!");
            return;
        }

//        if (TextUtils.isEmpty(platenum)) {
//            AbDialogUtil.removeDialog(DispatchCarAdd.this);
//            AbToastUtil.showToast(DispatchCarAdd.this, "车号不能为空!");
//            return;
//        }

//        if (TextUtils.isEmpty(unit)) {
//            AbDialogUtil.removeDialog(DispatchCarAdd.this);
//            AbToastUtil.showToast(DispatchCarAdd.this, "合同单位不能为空!");
//            return;
//        }

        if (TextUtils.isEmpty(cargoname)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "货名不能为空!");
            return;
        }

//        if (TextUtils.isEmpty(type)) {
//            AbDialogUtil.removeDialog(DispatchCarAdd.this);
//            AbToastUtil.showToast(DispatchCarAdd.this, "类型不能为空!");
//            return;
//        }

        if (TextUtils.isEmpty(sender)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "发货不能为空!");
            return;
        }

        if (TextUtils.isEmpty(receiver)) {
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "收货不能为空!");
            return;
        }

        mAddDispatchBtn.setEnabled(false);
        mSaveBtn.setEnabled(false);
        ArrayList<String> params = new ArrayList<>();
        params.add("id");
        params.add("platenum");
        params.add("cargoname");
        params.add("spec");
        params.add("type");
        params.add("sender");
        params.add("receiver");
        params.add("userName");
        params.add("plandId");
        params.add("unit");
        params.add("price");
        ArrayList<String> values = new ArrayList<>();
        values.add(id);
        values.add(platenum);
        values.add(cargoname);
        values.add(spec);
        values.add(type);
        values.add(sender);
        values.add(receiver);
        values.add(State.mName);
//        values.add(planId);
//        values.add(unit);
        values.add(price);
        String methodName = "updateDispatch";
        Listener<String> listener = response -> {
            mAddDispatchBtn.setEnabled(true);
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            JSONObject object = JsonHelper.getJsonObject(response);
            if (object != null && object.has("result")) {
                String value;
                try {
                    value = object.getString("result");
                    if ("true".equals(value)) {
                        AbToastUtil.showToast(DispatchCarAdd.this, "修改成功!");
                        setUpdateState(true);
                    } else {
                        AbToastUtil.showToast(DispatchCarAdd.this, "修改失败,已审核或已过磅的派车单不能修改，请查看派车状态!");
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                AbToastUtil.showToast(DispatchCarAdd.this, "修改失败,已审核或已过磅的派车单不能修改，请查看派车状态!");
            }
        };

        ErrorListener errorListener = error -> {
            mAddDispatchBtn.setEnabled(true);
            AbDialogUtil.removeDialog(DispatchCarAdd.this);
            AbToastUtil.showToast(DispatchCarAdd.this, "网络不给力，请重试!");
        };

        VolleyCallWebService webService = new VolleyCallWebService(methodName,
                params, values, listener, errorListener, Priority.NORMAL);
        VolleySingleton.getVolleyInstance(mAppContext).addRequest(webService);

    }

    /**
     * 修改成功
     */
    private void setUpdateState(boolean isTrue) {
        Intent i = new Intent();
        i.putExtra("updateSate", isTrue);
        setResult(RESULT_OK, i);
        DispatchCarAdd.this.finish();
    }

    /**
     * 刷新
     */
    private void refresh() {
//        mContractTxt.setText("");
        setOrderId();
        mPlatenumberTxt.setText("");
        mCargonameTxt.setText("");
        mSpecTxt.setText("");
        mTypeTxt.setText("");
        mSenderTxt.setText("");
        mReceiverTxt.setText("");
        mPriceTxt.setText("");
        mRawNet.setText("");
        mUnloadArea.setText("");
        mDriverName.setText("");
        mIDNumber.setText("");
        mDriverPhone.setText("");
        mOperatorTxt.setText(State.mName);
        mPlanId.setText("");
        mUnloadUnit.setText("");
        mBeizuTxt.setText("");
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        String value = data.getStringExtra("result");
        codeContent=data.getStringExtra(Constant.CODED_CONTENT);
        switch (requestCode) {
            case GETCARNUMBER:
                if (!TextUtils.isEmpty(value)) {
                    mPlatenumberTxt.setText(value);
                }
                break;
            case CARGONAME:
                mCargonameTxt.setText(value);
                break;
            case SPEC:
                mSpecTxt.setText(value);
                break;
            case TYPE:
                mTypeTxt.setText(value);
                break;
            case SENDER:
                mSenderTxt.setText(value);
                break;
            case RECEIVER:
                mReceiverTxt.setText(value);
                break;
            case GETPLAN:
                setPlan(data);
                break;
            case UNLOADAREA:
                mUnloadArea.setText(value);
                break;
            case UNLOADUNIT:
                mUnloadUnit.setText(value);
                break;
            case TOCODE:
//                getPlan();
                break;
            case TRANSPORT:
                /*
                 * if(value.equals("散装")) {
                 * mBucketNetLayout.setVisibility(View.GONE);
                 * mBagNetLayout.setVisibility(View.GONE); } else
                 * if(value.equals("袋装")) {
                 * mBucketNetLayout.setVisibility(View.GONE);
                 * mBagNetLayout.setVisibility(View.VISIBLE); } else
                 * if(value.equals("桶装")) {
                 * mBucketNetLayout.setVisibility(View.VISIBLE);
                 * mBagNetLayout.setVisibility(View.GONE); }
                 * mTransportTxt.setText(value);
                 */
                break;
            default:
        }
    }

    /**
     * 设置计划
     */
    private void setPlan(Intent i) {
        PlanModel model = i.getParcelableExtra("plan");
        if (model != null) {

            mPlanId.setText(model.getPlanId());
            mCargonameTxt.setText(model.getCargoname());
            mSenderTxt.setText(model.getSender());
            mReceiverTxt.setText(model.getReceiver());
            mSpecTxt.setText(model.getSpec());
            contractId=model.getPlanId();
        }
    }

    @Override
    public void getImgClick(int position) {

    }
}